package com.ml.mall.service.comparison.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.json.JsonMapper;
import com.ml.mall.bean.req.comparison.SysComparisonAddDto;
import com.ml.mall.bean.req.comparison.SysComparisonDelDto;
import com.ml.mall.bean.res.comparison.SysComparisonRes;
import com.ml.mall.enums.CommonEnum;
import com.ml.mall.enums.RedisUtilsCacheEnum;
import com.ml.mall.exception.BizException;
import com.ml.mall.mapper.comparison.SysComparisonMapper;
import com.ml.mall.security.JwtUtil;
import com.ml.mall.service.comparison.SysComparisonService;
import com.ml.mall.utils.ComparisonUtils;
import com.ml.mall.utils.RedisUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;


/**
 * @author :pl
 * @version :1.0
 * @description :Redis比对实现类
 * @date :2021/5/28
 **/
@Service
@Slf4j
public class SysComparisonServiceImpl implements SysComparisonService {

    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private SysComparisonMapper sysComparisonMapper;

    @Override
    public void sysComparisonAdd(SysComparisonAddDto dto) {
        //进行redis的添加,获取key值
        String curComparisonKey = RedisUtilsCacheEnum.REDIS_KEY_COMPARISON.getKeyPrefix() + ComparisonUtils.getCurComparisonKey();
        SysComparisonRes sysComparisonRes = sysComparisonMapper.selectRedisNeedAll(dto.getVariantId());
       Long size = redisUtil.lGetListSize(curComparisonKey);
        if(size >= 3){
            //推入
            //获取第0个；
            Object needDelValue = redisUtil.lGetIndex(curComparisonKey, 0);
            //删除
            redisUtil.lRemove(curComparisonKey,1,needDelValue);
            //插入
            String sysComparisonResStr = JSONObject.toJSONString(sysComparisonRes);
            log.info("转换后的json字符串格式为：{}",sysComparisonResStr);
            redisUtil.lSet(curComparisonKey,sysComparisonResStr);
        }else{
            //直接插入
            String sysComparisonResStr = JSONObject.toJSONString(sysComparisonRes);
            log.info("转换后的json字符串格式为：{}",sysComparisonResStr);
            redisUtil.lSet(curComparisonKey,sysComparisonResStr);
        }
    }


    @Override
    public List<SysComparisonRes> sysComparisonQuery() {
        List<SysComparisonRes> sysComparisonResList = new ArrayList<>();
        //进行redis的添加,获取当前用户的key值
        String curComparisonKey = ComparisonUtils.getCurComparisonKey();
        //首先使用jwt获取companyId
        Long companyId = JwtUtil.getCurrentUser().getCompanyId();
        //根据companyId获取companyName
        String companyName = sysComparisonMapper.selectCompanyNameById(companyId);

        //获取redis中存储的数据
        List<Object> queryNeedRedisData = redisUtil.lGet(curComparisonKey, 0, 2);
        //查询redis中数据的条数
        long getRedisSize = redisUtil.lGetListSize(curComparisonKey);

        //将获取的数据进行转换为SysComparisonRes
        for (Object item:queryNeedRedisData){
            SysComparisonRes sysComparisonRes = JSON.parseObject((String)item,SysComparisonRes.class);
            if (sysComparisonRes.getSupplierName().equals(companyName)){
                //如果供应商名称和公司名称相同就进行封装
                sysComparisonResList.add(sysComparisonRes);
            }
        }
        return sysComparisonResList;
    }

    @Override
    public void sysComparisonDel(SysComparisonDelDto dto) {
        //获取redis中的key值
        String curComparisonKey = ComparisonUtils.getCurComparisonKey();
        //判断redis中是否有存储信息
        boolean checkIfHasKey = redisUtil.hasKey(curComparisonKey);
        if (checkIfHasKey){
            //redis中存入数据的条数
            long redisDataSize = redisUtil.lGetListSize(curComparisonKey);
//            从redis中获取已存在数据信息
            List<Object> queryNeedRedisData = redisUtil.lGet(curComparisonKey, 0, redisDataSize-1);
//            将存入redis的信息转换为对象
            for(Object item :queryNeedRedisData){
                SysComparisonRes sysComparisonRes = JSON.parseObject((String)item,SysComparisonRes.class);
                //如果出现值相等的情况
                if (sysComparisonRes.getVariantId() == dto.getVariantId()) {
                    //执行删除
                    redisUtil.lRemove(curComparisonKey, 1, (String)item);
                }
            }
        }else {
            throw new BizException(CommonEnum.REDIS_DELETE_ERROR);
        }
    }
}
